package corp.sunny.sso.domain;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

/**
 * @File: SSOUserAcct.java
 * 
 * @Date: Jun 9, 2011
 * @Author: Subhash Bavi
 * @Description: SSOAccount is a domain business object which holds user
 *               credential's and GUID id's after successful user authentication
 * @Copyright Sunny Corporation - 2011
 */
@Entity
@Table(name = "SUNNY.USRACCT")
public class SSOUserAcct implements Serializable {

	private static final long serialVersionUID = 22775514905841217L;

	// Persistent Objects
	private Long id;
	private String user;
	private String passwd;

	// Many - Many relation with role
	private Set<SSOUserRole> roles = new HashSet<SSOUserRole>(0);

	// Exception error (Transient)
	private boolean isAuth = false;
	private String errCode;
	private String errDesc;

	@Id
	@GeneratedValue
	@Column(name = "ACCT_ID")
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name = "ACCT_USR", nullable = false)
	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	@Column(name = "ACCT_PASSWD", nullable = false)
	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	@OneToMany(cascade = CascadeType.ALL)
	@JoinTable(name = "SUNNY.ACCTROLEMAPPER", joinColumns = { @JoinColumn(name = "ACCT_ID") }, inverseJoinColumns = { @JoinColumn(name = "ROLE_ID") })
	@LazyCollection(LazyCollectionOption.FALSE)
	public Set<SSOUserRole> getRoles() {
		return roles;
	}

	public void setRoles(Set<SSOUserRole> roles) {
		this.roles = roles;
	}

	@Transient
	public String getErrCode() {
		return errCode;
	}

	public void setErrCode(String erroCode) {
		this.errCode = erroCode;
	}

	@Transient
	public String getErrDesc() {
		return errDesc;
	}

	public void setErrDesc(String errDesc) {
		this.errDesc = errDesc;
	}

	@Transient
	public boolean isAuth() {
		return isAuth;
	}

	public void setAuth(boolean isAuth) {
		this.isAuth = isAuth;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((passwd == null) ? 0 : passwd.hashCode());
		result = prime * result + ((user == null) ? 0 : user.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		SSOUserAcct other = (SSOUserAcct) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (passwd == null) {
			if (other.passwd != null)
				return false;
		} else if (!passwd.equals(other.passwd))
			return false;
		if (user == null) {
			if (other.user != null)
				return false;
		} else if (!user.equals(other.user))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "SSOUserAcct [id=" + id + ", user=" + user + ", passwd="
				+ passwd + ", roles=" + roles + ", isAuth=" + isAuth
				+ ", errCode=" + errCode + ", errDesc=" + errDesc + "]";
	}
}
